package com.ezops.poc.processor;

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ezops.poc.bo.ot.ClientOTEquityModel;
import com.ezops.trade.processing.partners.orchestrade.equity.OrchesTradeEquityModel;

@Component(value = "splitProcessor")
public class TradeSplitProcessor implements Processor {
	@Autowired
	protected ProducerTemplate producer;

	@Override
	public void process(Exchange exchange) throws Exception {

		List<OrchesTradeEquityModel> validTrades = new ArrayList<>();
		List<ClientOTEquityModel> invalidTrades = new ArrayList<>();

		List aggregatedTrades = exchange.getIn().getBody(List.class);

		for (Object trade : aggregatedTrades) {
			if (trade instanceof ClientOTEquityModel) {
				invalidTrades.add((ClientOTEquityModel) trade);
			} else {
				validTrades.add((OrchesTradeEquityModel) trade);
			}
		}
		if (validTrades != null && !validTrades.isEmpty()) {
			exchange.getIn().setBody(validTrades);
			producer.send("direct:validfile", exchange);
		}
		if (invalidTrades != null && !invalidTrades.isEmpty()) {
			exchange.getIn().setBody(invalidTrades);
			producer.send("direct:errors", exchange);
		}

	}

}
